1 using UnityEngine;
2 using
System.Collections.Generic;
3
4 namespace
Helper
5 {
6     
public class ScrollHelp : MonoBehaviour
7     {
8         
public List<Texture2D> textureHelps;
9
10         
private int Current;
11
12         
private Rect rect1;
13         
private Rect rect2;
14         
private Rect rect3;
15         
private Rect rect4;
16
17         
private Rect bounds;
18
19         
private float touchX;
20         
private float deltaX;
21         
private float offsetX;
22         
private bool isUpdate;
23         
private float step;
24         
private float statetime;
25         
private float[] rxs;
26
27         
public GameObject dot0;
28         
public GameObject dot1;
29         
public GameObject dot2;
30         
public GameObject dot3;
31         
public GameObject bigDot;
32
33         
public void Start()
34         {
35             rect1 =
new Rect(0, 0, 538, 322);
36             rect2 =
new Rect(538 + 10, 0, 538, 322);
37             rect3 =
new Rect(538 * 2 + 20, 0, 538, 322);
38             rect4 =
new Rect(538 * 3 + 30, 0, 538, 322);
39             bounds =
new Rect(128, 68, 538, 322);
40             rxs =
new float[] { 0, 0, 0, 0 };
41         }
42
43
44         
public void OnGUI()
45         {
46             GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity,
new Vector3(Screen.width / 800f, Screen.height / 480f, 1f));
47             GUI.BeginGroup(bounds);
48
49             
// scrollPosition = GUI.BeginScrollView(new Rect(0, 0, 538, 350), scrollPosition, new Rect(0, 0, 538 * 4 + 30, 330));
50
51             GUILayout.BeginHorizontal(GUILayout.Width(
538), GUILayout.Height(322));
52
53             GUI.DrawTexture(rect1, textureHelps[
0]);
54             GUI.DrawTexture(rect2, textureHelps[
1]);
55             GUI.DrawTexture(rect3, textureHelps[
2]);
56             GUI.DrawTexture(rect4, textureHelps[
3]);
57
58             GUILayout.EndHorizontal();
59
60             
// GUI.EndScrollView();
61
62             GUI.EndGroup();
63         }
64
65         
public void Update()
66         {
67             
if (Input.GetButtonDown("Fire1"))
68             {
69                 touchX = Input.mousePosition.x;
70                 isUpdate =
false;
71                 updateDots();
72             }
73             
else if (Input.GetButton("Fire1"))//su kien giu chuot
74             {
75                 deltaX = Input.mousePosition.x - touchX;
76
77                 rect1.x += deltaX;
78                 rect2.x += deltaX;
79                 rect3.x += deltaX;
80                 rect4.x += deltaX;
81
82                 touchX = Input.mousePosition.x;
83
84             }
85             
else if (Input.GetButtonUp("Fire1"))
86             {
87                 Current = currentBg(rect1.x);
88                 offsetX = -
538 * Current - 10 * Current - rect1.x;
89                 step = offsetX *
5;
90                 isUpdate =
true;
91                 rxs[
0] = rect1.x + offsetX;
92                 rxs[
1] = rect2.x + offsetX;
93                 rxs[
2] = rect3.x + offsetX;
94                 rxs[
3] = rect4.x + offsetX;
95             }
96
97
98             
if (isUpdate)
99             {
100                 
if (offsetX < 0)
101                 {
102                     
if (rect1.x > rxs[0])
103                     {
104                         rect1.x += step * Time.deltaTime;
105                         rect2.x += step * Time.deltaTime;
106                         rect3.x += step * Time.deltaTime;
107                         rect4.x += step * Time.deltaTime;
108                     }
109                     
else
110                     {
111                         rect1.x = rxs[
0];
112                         rect2.x = rxs[
1];
113                         rect3.x = rxs[
2];
114                         rect4.x = rxs[
3];
115                         isUpdate =
false;
116                         updateDots();
117                     }
118                 }
119                 
else
120                 {
121                     
if (rect1.x < rxs[0])
122                     {
123                         rect1.x += step * Time.deltaTime;
124                         rect2.x += step * Time.deltaTime;
125                         rect3.x += step * Time.deltaTime;
126                         rect4.x += step * Time.deltaTime;
127                     }
128                     
else
129                     {
130                         rect1.x = rxs[
0];
131                         rect2.x = rxs[
1];
132                         rect3.x = rxs[
2];
133                         rect4.x = rxs[
3];
134                         isUpdate =
false;
135                         updateDots();
136                     }
137                 }
138             }
139         }
140
141         
private int currentBg(float rect1X)
142         {
143             
if (rect1X >= -269)
144                 
return 0;
145             
else if (rect1X < -269 && rect1X >= -817)
146                 
return 1;
147             
else if (rect1X < -817 && rect1X >= -1355)
148                 
return 2;
149             
else
150                 
return 3;
151         }
152
153         
private void updateDots()
154         {
155             
switch (Current)
156             {
157                 
case 0:
158                     bigDot.transform.position =
new Vector3(dot0.transform.position.x, dot0.transform.position.y, bigDot.transform.position.z);
159                     
break;
160                 
case 1:
161                     bigDot.transform.position =
new Vector3(dot1.transform.position.x, dot1.transform.position.y, bigDot.transform.position.z);
162                     
break;
163                 
case 2:
164                     bigDot.transform.position =
new Vector3(dot2.transform.position.x, dot2.transform.position.y, bigDot.transform.position.z);
165                     
break;
166                 
case 3:
167                     bigDot.transform.position =
new Vector3(dot3.transform.position.x, dot3.transform.position.y, bigDot.transform.position.z);
168                     
break;
169             }
170         }
171     }
172 }


scrollPosition = GUI.BeginScrollView(new Rect(0, 0, 538, 350), scrollPosition, new Rect(0, 0, 538 * 4 + 30, 330));

GUI.EndScrollView();

else if (Input.GetButton("Fire1"))su kien giu chuot




Trò chơi đua xe động vật trong UNITY Engine 114.814 lượt xem

Gõ tìm kiếm nhanh...